//
//  ActivityDetailCell.m
//  Contacts
//
//  Created by 李 雷川 on 14-7-19.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ActivityDetailCell.h"
#import "DateMethod.h"
#import "JoinActivitySmallView.h"
#import "JoinActivity.h"
@implementation ActivityDetailCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self setupSubViews];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code


}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)layoutSubviews{
    
    
    float margin = 10;
    float paddingTop =20;
    float paddingLeft =20;
    float innerSpace = 5;
    float topHeigth =25;
    float areaSpace = 10;
    
     bgImageView.frame = CGRectMake(margin,margin,CGRectGetWidth(self.frame)- margin * 2, CGRectGetHeight(self.frame) - 2 * margin);
    
    float headWidth = 48;
    float headHeight = 48;
    headImageView.frame = CGRectMake(4,4,headWidth,headHeight);
    [headImageView setClipsToBounds:YES];
    [headImageView.layer setCornerRadius:headImageView.frame.size.width/2.0];
    
    CGSize size = [nameLabel sizeThatFits:CGSizeMake(100,topHeigth)];
    nameLabel.frame =CGRectMake(CGRectGetMaxX(headImageView.frame) + innerSpace,paddingTop,size.width,topHeigth);
    
    timeLabel.frame =CGRectMake(CGRectGetMaxX(nameLabel.frame)+innerSpace,paddingTop,100,topHeigth);

    
    


    
  
    
    lineImageView.frame = CGRectMake(CGRectGetMaxX(headImageView.frame),CGRectGetMaxY(nameLabel.frame) + innerSpace,CGRectGetWidth(bgImageView.frame) - CGRectGetMaxX(headImageView.frame) - innerSpace, 1);
    
    
    subjectLabel.frame = CGRectMake(paddingLeft,CGRectGetMaxY(lineImageView.frame) + areaSpace,CGRectGetWidth(bgImageView.frame) - 2 * paddingLeft,title_font);
    [subjectLabel sizeToFit];
    
    timeLogoImageView.frame = CGRectMake(paddingLeft,CGRectGetMaxY(subjectLabel.frame) + areaSpace + 2, 13,13);
    
    timeTitleLabel.frame = CGRectMake(CGRectGetMaxX(timeLogoImageView.frame) +innerSpace ,CGRectGetMaxY(subjectLabel.frame) + areaSpace,70,content_font);
    [timeTitleLabel sizeToFit];
    
    
    activityTimeLabel.frame = CGRectMake(CGRectGetMaxX(timeTitleLabel.frame) + innerSpace,CGRectGetMaxY(subjectLabel.frame) + areaSpace, 70,content_font);
    [activityTimeLabel sizeToFit];
    
    attentionBtn.frame = CGRectMake(CGRectGetWidth(bgImageView.frame) - 85,CGRectGetMaxY(subjectLabel.frame) + 8, 75,22);
    
    
    placeLogoImageView.frame = CGRectMake(paddingLeft,CGRectGetMaxY(timeLogoImageView.frame) + innerSpace + 3, 13,13);
    
    placeTitleLabel.frame = CGRectMake(CGRectGetMaxX(placeLogoImageView.frame) + innerSpace,CGRectGetMaxY(timeTitleLabel.frame) + innerSpace,70,content_font);
    [placeTitleLabel sizeToFit];
    
    placeLabel.frame = CGRectMake(CGRectGetMaxX(placeTitleLabel.frame)+innerSpace ,CGRectGetMaxY(activityTimeLabel.frame) + 5, 70,content_font);
    [placeLabel sizeToFit];
    
    detailLabel.frame = CGRectMake(paddingLeft,CGRectGetMaxY(placeTitleLabel.frame) + areaSpace/2,CGRectGetWidth(bgImageView.frame) - paddingLeft * 2,description_font);
    [detailLabel sizeToFit];
    
    
    joinNumLabel.frame =CGRectMake(paddingLeft,CGRectGetMaxY(detailLabel.frame)+areaSpace ,50,34);
    [joinNumLabel sizeToFit];
    
    CGRect scrollerFrame = CGRectMake(CGRectGetMaxX(joinNumLabel.frame)+ 20,CGRectGetMaxY(detailLabel.frame) + areaSpace,180, 34);
    joinScrollerView.frame = scrollerFrame;
    for (NSInteger i =0; i<activity.joinArray.count;i++) {
        JoinActivity *joinAcitivity =activity.joinArray[i];
        float space = 5;
        float width = 32;
        float height = 32;
        JoinActivitySmallView *view = [[JoinActivitySmallView alloc]initWithFrame:CGRectMake((width + space) * i, 0, width, height)];
        [view initWithJoinActivity:joinAcitivity];
        [joinScrollerView addSubview:view];
    }
    [joinScrollerView setContentSize:CGSizeMake(37 * activity.joinArray.count, 32)];
}

-(void)setupSubViews{
    //    self.contentView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.1];
    
    bgImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.image = [[UIImage imageNamed:@"activit_bg.png"]stretchableImageWithLeftCapWidth:50 topCapHeight:50];
    
    headImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    headImageView.image = [UIImage imageNamed:@"head.png"];
    
    nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    [nameLabel setNumberOfLines:1];
    nameLabel.font =[UIFont systemFontOfSize:content_font];
    [nameLabel setTextAlignment:NSTextAlignmentLeft];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    
    timeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [timeLabel setNumberOfLines:1];
    timeLabel.font =[UIFont systemFontOfSize:description_font];
    [timeLabel setTextAlignment:NSTextAlignmentLeft];
    [timeLabel setTextColor:[UIColor lightGrayColor]];
    [timeLabel setBackgroundColor:[UIColor clearColor]];
    
    joinNumLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [joinNumLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [joinNumLabel setNumberOfLines:1];
    joinNumLabel.font =[UIFont systemFontOfSize:content_font];
    [joinNumLabel setTextAlignment:NSTextAlignmentLeft];
    [joinNumLabel setTextColor:[UIColor redColor]];
    [joinNumLabel setBackgroundColor:[UIColor clearColor]];
    
    
    lineImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    lineImageView.image = [UIImage imageNamed:@"dot_line.png"];
    
    subjectLabel = [[UILabel alloc]initWithFrame:CGRectZero];;
    [subjectLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [subjectLabel setNumberOfLines:0];
    subjectLabel.font =[UIFont systemFontOfSize:title_font];
    [subjectLabel setTextAlignment:NSTextAlignmentLeft];
    [subjectLabel setBackgroundColor:[UIColor clearColor]];
    
    timeLogoImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [timeLogoImageView setContentMode:UIViewContentModeCenter];
    timeLogoImageView.image = [UIImage imageNamed:@"time_icon.png"];
    
    timeTitleLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [timeTitleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [timeTitleLabel setNumberOfLines:1];
    timeTitleLabel.font =[UIFont systemFontOfSize:content_font];
    [timeTitleLabel setTextAlignment:NSTextAlignmentLeft];
    [timeTitleLabel setTextColor:[UIColor blackColor]];
    [timeTitleLabel setBackgroundColor:[UIColor clearColor]];
    timeTitleLabel.text = @"参加时间:";
    
    activityTimeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [activityTimeLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [activityTimeLabel setNumberOfLines:1];
    activityTimeLabel.font =[UIFont systemFontOfSize:content_font];
    [activityTimeLabel setTextAlignment:NSTextAlignmentLeft];
    [activityTimeLabel setBackgroundColor:[UIColor clearColor]];
    
    placeLogoImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [placeLogoImageView setContentMode:UIViewContentModeCenter];
    placeLogoImageView.image = [UIImage imageNamed:@"place_icon.png"];
    
    placeTitleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    [placeTitleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [placeTitleLabel setNumberOfLines:1];
    placeTitleLabel.font =[UIFont systemFontOfSize:content_font];
    [placeTitleLabel setTextAlignment:NSTextAlignmentLeft];
    [placeTitleLabel setBackgroundColor:[UIColor clearColor]];
    placeTitleLabel.text = @"参加地点:";
    
    placeLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [placeLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [placeLabel setNumberOfLines:1];
    placeLabel.font =[UIFont systemFontOfSize:content_font];
    [placeLabel setTextAlignment:NSTextAlignmentLeft];
    [placeLabel setBackgroundColor:[UIColor clearColor]];
    
    
    detailLabel =  [[UILabel alloc]initWithFrame:CGRectZero];;
    [detailLabel setLineBreakMode:NSLineBreakByTruncatingTail];
    [detailLabel setNumberOfLines:0];
    detailLabel.font =[UIFont systemFontOfSize:description_font];
    [detailLabel setTextAlignment:NSTextAlignmentLeft];
    [detailLabel setTextColor:[UIColor darkGrayColor]];
    [detailLabel setBackgroundColor:[UIColor clearColor]];
    
    attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [attentionBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [attentionBtn setImage:[UIImage imageNamed:@"attention.png"] forState:UIControlStateNormal];
    
    
    joinScrollerView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    
    [self.contentView addSubview:bgImageView];
    [bgImageView addSubview:headImageView];
    [bgImageView addSubview:nameLabel];
    [bgImageView addSubview:timeLabel];
    [bgImageView addSubview:lineImageView];
    
    [bgImageView addSubview:joinNumLabel];
    [bgImageView addSubview:subjectLabel];
    
    [bgImageView addSubview:timeTitleLabel];
    [bgImageView addSubview:activityTimeLabel];
    [bgImageView addSubview:attentionBtn];
    [bgImageView addSubview:placeTitleLabel];
    [bgImageView addSubview:placeLabel];
    [bgImageView addSubview:detailLabel];
    [bgImageView addSubview:timeLogoImageView];
    [bgImageView addSubview:placeLogoImageView];
    [bgImageView addSubview:joinScrollerView];
    
}

-(void)initWithActivity:(Activity *)_activity{
    activity = _activity;
    
    nameLabel.text = activity.initiator.name;
    timeLabel.text = [DateMethod timestampFromString:activity.time];

    
    
    subjectLabel.text = activity.subject;
    
    activityTimeLabel.text = [NSString stringWithFormat:@"%@-%@",[DateMethod changeDateTohhmmString:activity.beginDate],[DateMethod changeDateTohhmmString:activity.endDate]];
    placeLabel.text = activity.place;
    detailLabel.text =[NSString stringWithFormat:@"详情:%@",activity.content] ;
    joinNumLabel.text =[NSString stringWithFormat:@"%lu人已报名",activity.joinArray.count];
    

}

@end
